package com.example.hhh.controller;

import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.http.ResponseEntity;
import java.util.HashMap;
import java.util.Map;
import org.springframework.transaction.TransactionSystemException;
import java.sql.SQLException;
import org.springframework.jdbc.UncategorizedSQLException;
import java.sql.SQLIntegrityConstraintViolationException;
import org.mybatis.spring.MyBatisSystemException;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(DataIntegrityViolationException.class)
    public ResponseEntity<Map<String, Object>> handleDataIntegrityViolationException(DataIntegrityViolationException ex) {
        Map<String, Object> result = new HashMap<>();
        result.put("success", false);
        result.put("message", "删除失败：存在关联数据，无法删除！");
        return ResponseEntity.ok(result);
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<Map<String, Object>> handleAllException(Exception ex) {
        Map<String, Object> result = new HashMap<>();
        Throwable cause = ex;
        // 递归查找 cause 链
        while (cause != null) {
            if (cause instanceof SQLIntegrityConstraintViolationException) {
                result.put("success", false);
                result.put("message", "删除失败：存在关联数据，无法删除！");
                return ResponseEntity.badRequest().body(result);
            }
            cause = cause.getCause();
        }
        // 其它异常
        result.put("success", false);
        result.put("message", "操作失败：" + ex.getMessage());
        return ResponseEntity.status(500).body(result);
    }

    @ExceptionHandler({TransactionSystemException.class, SQLException.class})
    public ResponseEntity<Map<String, Object>> handleDbException(Exception ex) {
        Map<String, Object> result = new HashMap<>();
        result.put("success", false);
        result.put("message", "删除失败：存在关联数据，无法删除！（数据库约束）");
        return ResponseEntity.ok(result);
    }

    @ExceptionHandler({UncategorizedSQLException.class})
    public ResponseEntity<Map<String, Object>> handleUncategorizedSQLException(UncategorizedSQLException ex) {
        Map<String, Object> result = new HashMap<>();
        if (ex.getCause() instanceof SQLIntegrityConstraintViolationException) {
            result.put("success", false);
            result.put("message", "删除失败：存在关联数据，无法删除！");
            return ResponseEntity.ok(result);
        }
        result.put("success", false);
        result.put("message", "操作失败：" + ex.getMessage());
        return ResponseEntity.status(500).body(result);
    }

    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public ResponseEntity<Map<String, Object>> handleSqlIntegrityConstraintViolationException(SQLIntegrityConstraintViolationException ex) {
        Map<String, Object> result = new HashMap<>();
        result.put("success", false);
        result.put("message", "删除失败：存在关联数据，无法删除！");
        return ResponseEntity.ok(result);
    }

    @ExceptionHandler(MyBatisSystemException.class)
    public ResponseEntity<Map<String, Object>> handleMyBatisSystemException(MyBatisSystemException ex) {
        Map<String, Object> result = new HashMap<>();
        Throwable cause = ex.getCause();
        while (cause != null) {
            if (cause instanceof SQLIntegrityConstraintViolationException) {
                result.put("success", false);
                result.put("message", "删除失败：存在关联数据，无法删除！");
                return ResponseEntity.ok(result);
            }
            cause = cause.getCause();
        }
        result.put("success", false);
        result.put("message", "操作失败：" + ex.getMessage());
        return ResponseEntity.status(500).body(result);
    }
} 